/**
 * 部门添加js
 */
var comValidform = null;
var returnStatus = false;
var bespeakCollection = {
	dataCache : null,

	startEvent : function() {
		comValidform = $(".com-addForm").Validform({
			tiptype : 2,
			tipSweep : true,
			beforeSubmit : function(curform) {
				var url = 'mgr/OrganizationCtrl/save';
				var params = jsUtils.serializeJson($('.com-addForm'));
				 
				params.parentID = bespeakCollection.getSelData();
				delete params.orgIds;
				delete params.selectId;
				var cb = function(data) {
					alert(data.msg);
					// 成功返回
					if (data.resultCode == 200) {
						// 关闭窗口并刷新数据
						art.dialog.close();
						returnStatus = true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});

		// 获取所有资源
		var url = 'mgr/OrganizationCtrl/listAllByUser';
		var params = {};
		var cb = function(data) {

			bespeakCollection.dataCache = data.result;
		};
		sendApiNoLoadSync(url, params, cb, 'GET');

		var orgIds = '';

		var orgCache = bespeakCollection.dataCache;
		for (var i = 0; i < orgCache.length; i++) {
			orgIds += orgCache[i].id + ',';
		}

		if (orgIds != null && orgIds != '') {
			orgIds = orgIds.substring(0, orgIds.length - 1);
			$('#com-addForm input[name="orgIds"]').val(orgIds);
		}

		bespeakCollection.showTreeData();
	},
	bindEvent: function() {
		//添加是否是负责人,默认不是
		
		$("#headerTr").hide();
		
		$("select[name='orgType']").change(function() {
			if ($("select[name='orgType']").val() != 4) {
				$("#headerTr").hide();
				 
				
			} else {
				$("#headerTr").show(); 
			}
		});
	},
	setCheck : function() {
		var zTree = $.fn.zTree.getZTreeObj("orgTree"), py = "", sy = "s", pn = "", sn = "s", type = {
			"Y" : py + sy,
			"N" : pn + sn
		};
		zTree.setting.check.chkboxType = type;
	},

	showTreeData : function() {
		var setting = {
			check : {
				enable : true,
				chkStyle : "checkBox",
				radioType : "all"
			},
			data : {
				simpleData : {
					enable : true
				}
			},
			view : {
				nameIsHTML : true
			},
			callback : {
				onCheck : function(event, treeId, treeNode){
					var zTree = $.fn.zTree.getZTreeObj("orgTree");
					var nodes = zTree.getCheckedNodes(); // 取得所有选中的
					var len = nodes.length;
					
					var num = 0;
					for(var i=0; i < nodes.length; i++){
						if(nodes[i].orgType == 5){
							num++
						}
					}
					var content = "";
					if(num > 0){
						content = "已选中" + num  + "个理财师";
								
					}else{
						content = "一个理财师也未选中！"
					}
					
					$("#selectIds").html(content);
				}
			}
		}; 
		
		var zNodes = [];
		var dataCache = bespeakCollection.dataCache;
		var resourceIds = $('#com-addForm input[name="orgIds"]').val();
		for (var i = 0; i < dataCache.length; i++) {
			zNodes[i] = {
				id : dataCache[i].id,
				name : dataCache[i].name,
				pId : dataCache[i].parentID,
				orgType:dataCache[i].orgType
			};
			if (i == 0) {
				zNodes[i].open = true;
			}
			/*
			 * if(resourceIds!=null){
			 * 
			 * var orgIds=resourceIds.split(',');
			 * 
			 * for(var j=0;j<orgIds.length;j++){ alert(zNodes[i].id +" " +
			 * zNodes[i].name + " " + orgIds[j]); if(zNodes[i].id==orgIds[j]){
			 * zNodes[i].checked=true; continue; } } }
			 */
		}

		$.fn.zTree.init($("#orgTree"), setting, zNodes);
		bespeakCollection.setCheck();
	},

	// 选择返回事件

	getSelData : function() {
		var zTree = $.fn.zTree.getZTreeObj("orgTree");
		var nodes = zTree.getCheckedNodes(); // 取得所有选中的
		var len = nodes.length;
		if (len != 1) {
			return null;
		}
		return nodes[0].id;
	},

	init : function() {
		bespeakCollection.startEvent();
		bespeakCollection.bindEvent();
	}
}
$(document).ready(function() {
	bespeakCollection.init();
});
function submitForm() {
	comValidform.submitForm();
	return returnStatus;
}